package com.huizhonglm.diap.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.AddrInfo;
import com.huizhonglm.diap.presenters.user.AddrListPersenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.user.iView.AddrListIView;
import com.huizhonglm.diap.widget.LoadView;

import java.util.List;

public class AddrListActivity extends BaseActivity implements AddrListIView,
        LoadView.OnLoadAgainListener, AdapterView.OnItemClickListener {

    private Context mContext;

    private LoadView mLoadView;
    private ListView mListView;

    private AddrListAdapter mAdapter;

    private AddrListPersenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addr_list);
        setBackEvent(this, null);
        mContext = AddrListActivity.this;
        mPresenter = new AddrListPersenter(this);
        assignViews();
        onLoadAgain();
    }

    @Override
    protected void assignViews() {
        mLoadView = (LoadView) findViewById(R.id.load_view);
        mListView = (ListView) findViewById(R.id.addr_list);
        Button newAddrBtn = (Button) findViewById(R.id.new_addr_btn);
        mLoadView.setDataView(mListView, this);
        mListView.setOnItemClickListener(this);
        newAddrBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 添加新地址
                Intent intent = new Intent(mContext, AddrMgrActivity.class);
                startActivityForResult(intent, 0);
            }
        });
    }

    @Override
    public void onLoadAgain() {
        mLoadView.showProgress();
        mPresenter.loadAddrList();
    }

    @Override
    public void onLoadAddrSuccess(List<AddrInfo> list) {
        if (!EmptyUtil.isEmpty(list)) {
            updatePage(list);
        } else {
            mLoadView.showEmptyView("您还没有添加收货地址呢");
        }
    }

    @Override
    public void  onLoadAddrEmpty() {
        mLoadView.showEmptyView("您还没有添加收货地址呢");
    }

    @Override
    public void onLoadAddrFailure() {
        mLoadView.showErrorView();
    }

    private void updatePage(List<AddrInfo> data) {
        if (mAdapter == null) {
            mAdapter = new AddrListAdapter(mContext, data);
            mListView.setAdapter(mAdapter);
        } else {
            mAdapter.setData(data);
            mAdapter.notifyDataSetChanged();
        }
        mLoadView.showDataView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            onLoadAgain();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AddrInfo info = (AddrInfo) parent.getAdapter().getItem(position);
        if (info != null) {
            Intent intent = new Intent(mContext, AddrMgrActivity.class);
            intent.putExtra("addrInfo", info);
            startActivityForResult(intent, 1);
        }
    }
}
